--
-- Author: mm
-- Date: 2018-03-24 11:04:06
--
-- NewActivityDungeonInfoView
-- 活动副本说明UI

local rewardNum = 3 -- 奖杯奖励个数、额外奖励个数

local panelItemWidth = 0 -- 整个奖励panel宽
local panelItemHeight = 0 -- 整个奖励panel高

local trophyRewardsOringinPosY = 0 -- 奖杯奖励item原始posY
local trophyRewardsPanelHeight = 0 -- 奖杯奖励panel高
local rewardOffsetX = 0 -- 额外奖励X间距
local itemWidth = 0 -- 额外奖励item宽

local IconRender = require("app.widget.renders.IconRender")
local NewRareInfoView = require("app.newBag.views.NewRareInfoView")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.NewActivityDungeonInfoScene

function ClassRef:init()
	self._ui.Button_close:addClickEventListener(function()
			executeFunc(self._onCloseView, self)
		end)

	self._tltText = self._ui["Panel/Panel_left/tlt"] -- 副本名字
	self._descText = self._ui["Panel/Panel_left/Text"] -- 副本说明
	self._icon = self._ui["Panel/Panel_left/Image"] -- 副本底图

	self._ruleText = self._ui["Panel/Panel_right/Panel_text/text"] -- 规则说明文本

	panelItemWidth, panelItemHeight = self._ui["Panel/Panel_right/Panel_item"]:getContentSize2()
	trophyRewardsOringinPosY = self._ui["Panel/Panel_right/Panel_item/Panel_1"]:getPositionY()
	trophyRewardsPanelHeight = self._ui["Panel/Panel_right/Panel_item/Panel_1"]:getContentSize().height
	self._trophyRewards = { 0, 0, 0 } -- 奖杯奖励
	for i = 1, rewardNum do
		local renderTemp = IconRender.create( self._ui["Panel/Panel_right/Panel_item/Panel_"..i]:getChildByName("icon") )
		renderTemp:setTipEnable(true)
		self._trophyRewards[i] = renderTemp
	end

	self._extraReward = { 0, 0, 0 } -- 额外奖励
	for i = 1, rewardNum do
		local renderTemp = IconRender.create( self._ui["Panel/Panel_right/Panel_item/icon_"..i] )

		itemWidth = renderTemp:getContentSize2()
		itemWidth = itemWidth * self._ui["Panel/Panel_right/Panel_item/icon_"..i]:getScale()

		renderTemp:setTipEnable(true)
		self._extraReward[i] = renderTemp
	end
	rewardOffsetX = self._ui["Panel/Panel_right/Panel_item/icon_2"]:getPositionX() - self._ui["Panel/Panel_right/Panel_item/icon_1"]:getPositionX() - itemWidth

	-- 宝箱说明tips
	self._ui["Panel/Panel_right/Panel_box/Panel_icon/lock"]:setVisible( false )
	self._ui["Panel/Panel_right/Panel_box/Panel_icon/mask"]:setVisible( false )
	display.uiAddClick(self._ui["Panel/Panel_right/Panel_box/Panel_icon/icon_2"], function()
		local dataTemp = GD:queryRestrictedRewardShowById( 4 )
		self:_showRareInfo( dataTemp, self._ui["Panel/Panel_right/Panel_box/Panel_icon/icon_2"] )
	end)

	-- 宝箱点击响应
	display.uiAddClick(self._ui["Panel/Panel_right/Panel_box/Panel_icon/icon"], function()
		executeFunc(self._onOperate, "activityDungeons_getReward")
	end)

	self._activityDungeonBar = self._ui["Panel/Panel_right/Panel_box/bar_1/LoadingBar"] -- 活动宝箱进度条
	self._activityDungeonExp = self._ui["Panel/Panel_right/Panel_box/exp"] -- 活动宝箱经验值
end

function ClassRef:show(index)
	self:setVisible(true)
end

-- data -- event_mode 表ID
function ClassRef:refresh( data )
	local eventModeConf = GD:queryEventModeById( data )
	if not eventModeConf then
		print( " ------------------>>> 找不到event_mode表中内容", data )
		return
	end
	local iconBgPath = GD:getImagePath( Res.teamtarget_pic, eventModeConf.eventBg)
	self._icon:loadTexture( iconBgPath )

	self._descText:setString( L(eventModeConf.eventTips) )
	self._ruleText:setString( L(eventModeConf.eventDes) )

	-- 以下：处理奖杯奖励部分
	for i = 1, rewardNum do -- UI上1~3=金、银、铜。配置表刚好相反
		self._trophyRewards[i]:setData( UD:parseRewardToBagItem( eventModeConf["reward".. ( rewardNum+1-i )] ) )
	end
	-- 以下：处理额外奖励部分
	local extraRewardList = UD:parseItemsFromString( eventModeConf.dropShow ) -- 额外奖励列表
	local extraRewardNum = #extraRewardList
	for i = 1, rewardNum do
		local rewardData = nil
		if i <= extraRewardNum then
			rewardData = extraRewardList[i]
		end
		self._extraReward[i]:setData( rewardData )
		self._extraReward[i]:setVisible( i <= extraRewardNum )
	end
	-- 以下：调整奖杯奖励posY位置
	self._ui["Panel/Panel_right/Panel_item/Text_0"]:setVisible( 0 < extraRewardNum )
	local trophyRewardsPosY = trophyRewardsOringinPosY
	if 0 >= extraRewardNum then -- 没有额外奖励？
		trophyRewardsPosY = panelItemHeight/2 - trophyRewardsPanelHeight/2
	end
	for i = 1, rewardNum do
		self._ui["Panel/Panel_right/Panel_item/Panel_"..i]:setPositionY( trophyRewardsPosY )
	end
	-- 以下：调整额外奖励posX位置
	if 0 < extraRewardNum then -- 有额外奖励？
		if 0 == extraRewardNum%2 then -- 偶数个消耗
			local leftMid = extraRewardNum/2 -- 左侧标记item
			local leftMidPosX = panelItemWidth/2 - itemWidth - rewardOffsetX/2 -- 左侧标记item坐标X
			self._extraReward[ leftMid ]:setPositionX( leftMidPosX )
			for i = leftMid - 1, 1, -1 do
				local posTemp = leftMidPosX - ( itemWidth + rewardOffsetX ) * ( leftMid - i )
				self._extraReward[ i ]:setPositionX( posTemp )
			end

			local rightMid = extraRewardNum/2 + 1 -- 右侧标记item
			local rightMidPosX = panelItemWidth/2 + rewardOffsetX/2 -- 右侧标记item坐标X
			self._extraReward[ rightMid ]:setPositionX( rightMidPosX )
			for i = rightMid + 1, extraRewardNum do
				local posTemp = rightMidPosX + ( itemWidth + rewardOffsetX ) * ( i - rightMid )
				self._extraReward[ i ]:setPositionX( posTemp )
			end
		else
			local midNum = ( extraRewardNum + 1 ) / 2
			local midIdxPosX = panelItemWidth/2 - itemWidth/2
			self._extraReward[ midNum ]:setPositionX( midIdxPosX )
			for i = midNum - 1, 1, -1 do -- 左侧item
				local posTemp = midIdxPosX - ( itemWidth + rewardOffsetX ) * ( midNum - i )
				self._extraReward[ i ]:setPositionX( posTemp )
			end
			for i = midNum + 1, extraRewardNum do
				local posTemp = midIdxPosX + ( itemWidth + rewardOffsetX ) * ( i - midNum )
				self._extraReward[ i ]:setPositionX( posTemp )
			end
		end
	end
end

-- 设置玩法名字
function ClassRef:setDungeonName( nameStr )
	nameStr = nameStr or ""
	self._tltText:setString( nameStr )
end

-- curActivityDungeonsPoint = 活动玩法当前活动点
function ClassRef:refreshBoxInfo( curActivityDungeonsPoint )
	local oneBoxNeedPoint = tonumber( GD:getSystemPar( "eventBoxPointRequired", 200 ) ) -- 一个包厢需要的点数
	local curAccumulateRewardNum = math.floor( curActivityDungeonsPoint/oneBoxNeedPoint ) -- 累积的个数
	local restPoint = curActivityDungeonsPoint%oneBoxNeedPoint -- 剩余点数

	-- 宝箱累积奖励个数
	self._ui["Panel/Panel_right/Panel_box/Panel_icon/bg_num/num"]:setString( curAccumulateRewardNum )
	self._ui["Panel/Panel_right/Panel_box/Panel_icon/bg_num"]:setVisible( 0 < curAccumulateRewardNum )

	local percent = math.floor( restPoint/oneBoxNeedPoint * 100 )
	self._activityDungeonBar:setPercent( percent )
	self._activityDungeonExp:setString( table.concat( { restPoint, "/", oneBoxNeedPoint } ) )

	self._ui["Panel/Panel_right/Panel_box/Panel_icon/icon"]:setVisible( 0 < curAccumulateRewardNum )
	self._ui["Panel/Panel_right/Panel_box/Panel_icon/icon_2"]:setVisible( 0 >= curAccumulateRewardNum )

	if 0 < curAccumulateRewardNum then
		self:playAction( "animation", true )
	else
		self:pauseAction()
	end
end

-- 展示上方宝物具体信息
function ClassRef:_showRareInfo( dataTemp, triggerNode )
	if self._rareInfoPanel then
		self._rareInfoPanel:setData( dataTemp )
	else
		self._rareInfoPanel = NewRareInfoView.create()
		self._rareInfoPanel:setData( dataTemp )
		self._ui["Panel"]:addChild( self._rareInfoPanel )

		-- 点击空白区域关闭
		display.nodeRemoveForNotHit(self._rareInfoPanel, function()
			if self._rareInfoPanel then
				self._rareInfoPanel:removeFromParent()
				self._rareInfoPanel = nil
			end
		end)
	end

	local triggerNodeBoundingBox = triggerNode:getBoundingBox()
	local worldPosTemp = triggerNode:getParent():convertToWorldSpace( cc.p( triggerNodeBoundingBox.x, triggerNodeBoundingBox.y ) )
	triggerNodeBoundingBox.x = worldPosTemp.x
	triggerNodeBoundingBox.y = worldPosTemp.y
	display.adjustNodePos( triggerNodeBoundingBox, self._rareInfoPanel )
end

return ClassRef
